<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require('./classes/control/control_faq_comment.php');
class Faq_Comment
{
	var $faq_comment_c;
	var $webType;//网站类型
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	
	public function __construct()
	{
	
		$this->faq_comment_c = new FaqCommentControl();
		$this->webType = empty($_REQUEST['web_type'])?1:(int)trim($_REQUEST['web_type']);
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{
		$url_info = $comment_info = array();
		//分页处理
		$page = 1;
		if (!empty($_REQUEST['page'])) 
		{
			$page = (int)$_REQUEST['page'];
		}
		$page_size = 20;
		if($_POST['search'] || !empty($_REQUEST['url_page']) )//搜索以及分页
		{
			$search_array = array
			(
			'com_type' => trim($_REQUEST['co_type']),
			'com_id' => trim($_REQUEST['co_id']),
			'com_title' => !empty($_POST['co_title'])?trim($_POST['co_title']):urldecode(trim($_GET['co_title'])),
			'com_content' => trim($_REQUEST['co_content']),
			'cbs_id' => (int)(trim($_REQUEST['cbs_id'])),
			'start_date'=>strtotime(trim($_REQUEST['start_date'])),
			'end_date'=>strtotime(trim($_REQUEST['end_date']))
			);
			$url_info = $this->faq_comment_c->get_url_info($page,'list_s',$page_size,$this->webType,$search_array);
			$comment_info = $this->faq_comment_c->get_comment_info('list_s',$url_info['page_info']['start'],$page_size,$this->webType,$search_array);
		}else
		{
			$url_info = $this->faq_comment_c->get_url_info($page,'list',$page_size,$this->webType);
			$comment_info = $this->faq_comment_c->get_comment_info('list',$url_info['page_info']['start'],$page_size,$this->webType);
		}
		$GLOBALS['smarty']->assign('web_type',   $this->webType);
		$GLOBALS['smarty']->assign('start_date',   $_REQUEST['start_date']);
		$GLOBALS['smarty']->assign('end_date',   $_REQUEST['end_date']);
		$GLOBALS['smarty']->assign('co_title',   $_REQUEST['co_title']);
		$GLOBALS['smarty']->assign('co_type',   $_REQUEST['co_type']);
		$GLOBALS['smarty']->assign('co_id',   $_REQUEST['co_id']);
		$GLOBALS['smarty']->assign('co_content',   $_REQUEST['co_content']);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('comment_info',$comment_info );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('faq_comment_index.htm'); 
	
	
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	
	public function add_edit()
	{	
	
		if($_REQUEST['act'] == 'add' && empty($_REQUEST['comment_id']))
		{
			if($_POST['sub_from'])
			{
				$insert_array = $this->set_add_edit($_POST);
				$comment_id = $this->faq_comment_c->operate_comment('add',$insert_array);
				if(!empty($comment_id))
				{
					echo  '<script type="text/javascript"> alert("增加成功！");
					window.location.href="/wondershare/faq_comment.php?action=add_edit&act=edit&comment_id='.$comment_id.'&web_type='.$this->webType.'"; </script>';
					exit;
				}
			}
			$GLOBALS['smarty']->assign('tile',     '添加');
			$GLOBALS['smarty']->assign('web_type',   $this->webType);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));	
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('product_name',     $product_name);
			$GLOBALS['smarty']->display('faq_comment_info.htm');
		}
		elseif($_REQUEST['act'] == 'edit' && !empty($_REQUEST['comment_id']))
		{
			$comment_info = array();
			if($_POST['sub_from'])
			{
				$edit_array = $this->set_add_edit($_POST);
				
				$affect = $this->faq_comment_c->operate_comment('edit',$edit_array);
				if($affect == 1)
				{
					echo  '<script type="text/javascript"> alert("编辑成功！");
					window.location.href="/wondershare/faq_comment.php?action=add_edit&act=edit&comment_id='.$_POST['comment_id'].'&web_type='.$this->webType.'"; </script>';
				}
			}
			$comment_info = $this->faq_comment_c->get_comment_id_info((int)$_GET['comment_id']);
			$GLOBALS['smarty']->assign('web_type',   $this->webType);
			
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('comment_info',$comment_info[0] );
			$GLOBALS['smarty']->display('faq_comment_info.htm'); 	
		}
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->faq_comment_c->delete_comment($checkList);
		}
	}
	
	function set_add_edit($_POST)
	{
		if($_POST['co_type'] == 2)
		{
			$com_rating = $_POST['co_rating'];
			$com_useful = 0;
		}
		else
		{
			$com_useful = $_POST['co_useful'];
			$com_rating = 0;
		}
		$add_edit_array = array
		(
			'com_id' => trim($_POST['comment_id']),
			'new_id' => trim($_POST['co_new_id']),
			'com_rating' => $com_rating,
			'com_useful' => $com_useful,
			'com_status' => trim($_POST['co_status']),
			'com_content' => trim($_POST['co_content']),
			'com_type' => trim($_POST['co_type']),
			'title' => trim($_POST['article_name']),
			'name' => trim($_POST['co_name']),
			'email' => trim($_POST['co_email'])
		);
		
		return $add_edit_array;
	}


	/**
	* 弹出框显示
	* @param  NULL
	* @return  string
	*/
	public function faq_list()
	{
	
		//分页处理
		$page = 1;
		if (!empty($_REQUEST['page'])) 
		{
			$page = (int)$_REQUEST['page'];
		}
		$page_size = 20;
		$faq_title = !empty($_GET['faq_title'])?urldecode(trim($_GET['faq_title'])):trim($_POST['faq_title']);
		$search_array = array
		(
			'faq_type' => (int)($_REQUEST['faq_type']),
			'faq_title' => $faq_title
		);
		$url_info = $this->faq_comment_c->get_url_info($page,'faq_list',$page_size, $this->webType,$search_array);
		$faq_info  = $this->faq_comment_c->get_faq_list($url_info['page_info']['start'],$page_size, $this->webType,$search_array);
		
		$GLOBALS['smarty']->assign('faq_type',   trim($_REQUEST['faq_type']));
		$GLOBALS['smarty']->assign('web_type',   $this->webType);
		$GLOBALS['smarty']->assign('faq_title',   $faq_title);
		$GLOBALS['smarty']->assign('co_type',     $_REQUEST['co_type']);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->assign('faq_info',     $faq_info);
		$GLOBALS['smarty']->assign('co_new_id',     $_REQUEST['co_new_id']);
		$GLOBALS['smarty']->display('faq_list.htm'); 
	}
	/**
	* 模板页面提交评论ajax
	* @param  NULL
	* @return  string
	*/
	function tem_insert_comment()
	{
		$sql ="INSERT INTO faq_commet (new_id,com_content,com_type,com_useful,com_rating,com_status,update_time,update_user,title,com_time,web_type ) 
		VALUES ('{$_POST['new_id']}','".addslashes($_POST['contents'])."','1','{$_POST['is_helpful']}',
		'0','0',UNIX_TIMESTAMP(),'user from page','".addslashes($_POST['title'])."',UNIX_TIMESTAMP() , '".$this->webType."') ";
		echo  $GLOBALS['db']->query($sql);
	}
	/**
	* @函数export_excel,导入Excel
	* @param  NULL
	* @return  string
	*/
	function export()
	{
		$field_list_checkbox = "";
		if($_POST['sub_from'])
		{
			$filename = trim($_POST['file_name']);//文件名
			$file_type = "vnd.ms-excel"; 
			header("Content-Type: application/$file_type"); 
			header("Content-Disposition: attachment; filename=$filename"); 
			header("Pragma: no-cache"); 
			header("Expires: 0"); 
			echo  iconv('utf-8', 'gbk',"评论主键\t");echo  iconv('utf-8', 'gbk',"FAQ主键\t");echo  iconv('utf-8', 'gbk',"文章标题\t");
			echo  iconv('utf-8', 'gbk',"评论内容\t");echo  iconv('utf-8', 'gbk',"评论时间\t");echo  iconv('utf-8', 'gbk',"评论类型\t");
			echo  iconv('utf-8', 'gbk',"评论人名称\t");echo  iconv('utf-8', 'gbk',"评论人邮件\t");echo  iconv('utf-8', 'gbk',"是否有用\t");
			echo  iconv('utf-8', 'gbk',"修改人\t");echo  iconv('utf-8', 'gbk',"修改时间\t");
			echo "\n";
			$sql = "SELECT  com_id,new_id,title,com_content,IF(com_time > 0, 
			FROM_UNIXTIME(com_time, '%Y-%m-%d %H:%i:%s'), '') com_time ,com_type,com_useful,
			IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d %H:%i:%s'), '') update_time,
			com_name,com_email,update_user  FROM  faq_commet  
			WHERE com_time >= '".strtotime(trim($_POST['start_date']))."'AND
			com_time <= '".strtotime(trim($_POST['end_date']))."' AND web_type = '".$this->webType."' 
			AND com_content!='' " ; 
			$result = $GLOBALS['db']->query($sql);
			while ($rows = $GLOBALS['db']->fetch_array($result))
			{
				echo $rows['com_id']."\t";
				echo $rows['new_id']."\t";
				echo iconv('utf-8', 'gbk',$this->replace_html(trim($rows['title'])))."\t";
				echo iconv('utf-8', 'gbk',$this->replace_html(trim($rows['com_content'])))."\t";
				echo $rows['com_time']."\t";
				if($rows['com_type'] == 1 )
				{
					echo iconv('utf-8', 'gbk',"文章评论\t");
				}elseif($rows['com_type'] == 2 )
				{
					echo iconv('utf-8', 'gbk',"视频评论\t");
				}
				elseif($rows['com_type'] == 3 )
				{
					echo iconv('utf-8', 'gbk',"'在线帮助评论\t");
				}
				echo $rows['com_name']."\t";
				echo $rows['com_email']."\t";
				if($rows['com_useful'] == 1 )
				{
					echo iconv('utf-8', 'gbk',"有用\t");
				}elseif($rows['com_useful'] == 0 )
				{
					echo iconv('utf-8', 'gbk',"无用\t");
				}
				
				echo $rows['update_user']."\t";
				echo $rows['update_time']."\t";
				echo "\n";
			}
			exit;
		
		}
		$GLOBALS['smarty']->assign('web_type',   $this->webType);
		$GLOBALS['smarty']->display('comment_export.htm'); 
	}
	/**
	* @函数replace_html,替换特殊的字符
	* @param  NULL
	* @return  string
	*/
	function replace_html($str)  
	{  
		$str = trim($str);  
		$str=preg_replace("{\t}","",$str);  
		$str=preg_replace("{\r\n}","",$str);  
		$str=preg_replace("{\r}","",$str);  
		$str=preg_replace("{\n}","",$str);  
		return $str;  
	} 

}
$action    = $_REQUEST['action']; 
$faq_comment   = new Faq_Comment;
if(method_exists($faq_comment, $action)) 
{
	$faq_comment->$action();
}else{
	$faq_comment->index();
}

?>
